package work.soho.proxy.aliyun.parts;

import com.aliyuncs.ecs.model.v20140526.*;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class ServerInstancePart extends BasePart implements IPart{    private String accessSecret = "";

    private static final String INSTANCE_STATUS_RUNNING = "Running";
    private static final int INSTANCE_STATUS_CHECK_INTERVAL_MILLISECOND = 3000;
    private static final long INSTANCE_STATUS_TOTAL_CHECK_TIME_ELAPSE_MILLISECOND = 60000 * 3;

    /**
     * 是否只预检此次请求。true：发送检查请求，不会创建实例，也不会产生费用；false：发送正常请求，通过检查后直接创建实例，并直接产生费用
     */
    private static final boolean dryRun = false;

    @Override
    public boolean isExist() {
        return getId() != null;
    }

    @Override
    public String getId() {
        DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest();
        describeInstancesRequest.setRegionId(regionId);
        describeInstancesRequest.setInstanceName(configure.getInstanceName());
        DescribeInstancesResponse describeInstancesResponse = callOpenApi(describeInstancesRequest);
        if(describeInstancesResponse != null && describeInstancesResponse.getTotalCount()>0) {
            return describeInstancesResponse.getInstances().get(0).getInstanceId();
        }
        return null;
    }

    /**
     * 获取服务器公网IP
     * @return
     */
    public String getWIp() {
        DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest();
        describeInstancesRequest.setRegionId(regionId);
        describeInstancesRequest.setInstanceName(configure.getInstanceName());
        DescribeInstancesResponse describeInstancesResponse = callOpenApi(describeInstancesRequest);
        if(describeInstancesResponse != null && describeInstancesResponse.getTotalCount()>0) {
            return describeInstancesResponse.getInstances().get(0).getPublicIpAddress().get(0);
        }
        return null;
    }

    @Override
    public void drop() {
        DeleteInstanceRequest deleteInstanceRequest = new DeleteInstanceRequest();
        deleteInstanceRequest.setRegionId(regionId);
        deleteInstanceRequest.setInstanceId(getId());
        callOpenApi(deleteInstanceRequest);
    }

    @Override
    public void create() throws AliyunPartsException {
        RunInstancesRequest runInstancesRequest = new RunInstancesRequest();
        runInstancesRequest.setDryRun(dryRun);
        runInstancesRequest.setInstanceType(configure.getInstanceType());
        runInstancesRequest.setInstanceChargeType(configure.getInstanceChargeType());
        runInstancesRequest.setImageId(configure.getImageId());
        runInstancesRequest.setSecurityGroupId(configure.getSecurityGroupId());
        runInstancesRequest.setPeriod(configure.getPeriod());
        runInstancesRequest.setPeriodUnit(configure.getPeriodUnit());
        runInstancesRequest.setZoneId(configure.getZoneId());
        runInstancesRequest.setInternetChargeType(configure.getInternetChargeType());
        runInstancesRequest.setVSwitchId(configure.getVSwitchId());
        runInstancesRequest.setInstanceName(configure.getInstanceName());
        runInstancesRequest.setAmount(configure.getAmount());
        runInstancesRequest.setInternetMaxBandwidthOut(configure.getInternetMaxBandwidthOut());
        runInstancesRequest.setIoOptimized(configure.getIoOptimized());
        runInstancesRequest.setKeyPairName(configure.getKeyPairName());
        runInstancesRequest.setSpotStrategy(configure.getSpotStrategy());
        runInstancesRequest.setSecurityEnhancementStrategy(configure.getSecurityEnhancementStrategy());
        runInstancesRequest.setSystemDiskSize(configure.getSystemDiskSize());
        runInstancesRequest.setSystemDiskCategory(configure.getSystemDiskCategory());

        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, (c.get(Calendar.HOUR_OF_DAY) + 1));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        runInstancesRequest.setAutoReleaseTime(sdf.format(c.getTime()));

        System.out.println(runInstancesRequest.toString());

        RunInstancesResponse runInstancesResponse = callOpenApi(runInstancesRequest);
        if(runInstancesResponse == null) {
            throw new AliyunPartsException("创建服务器失败， 请检查相关参数是否正确");
        }
    }

    @Override
    public void start() {

    }

    @Override
    public void configure() {

    }
}
